package com.thesis.client;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

import com.thesis.conectify.Connectify;
import com.thesis.internet.InternetManager;
import com.thesis.object.ListDishes;
import com.thesis.utils.Defines;
import com.thesis.utils.Utils;

public class SettingActivity extends PreferenceActivity {
	SharedPreferences settings;
	private Preference update_database;
	private ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		settings = getSharedPreferences(Defines.PREFS_NAME, 0);

		ActionBar ab = getActionBar();
		ab.setTitle(getResources().getString(R.string.title_activity_setting));
		ab.setDisplayHomeAsUpEnabled(true);

		addPreferencesFromResource(R.xml.setting);

		update_database = (Preference) findPreference("database_version");

		update_database
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						// TODO Auto-generated method stub
						if (!InternetManager.sharedInstance()
								.isInternetConnected(SettingActivity.this)) {
							OnClickListener negButListener = new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									startActivity(new Intent(
											android.provider.Settings.ACTION_WIRELESS_SETTINGS));
								}
							};
							OnClickListener posButListener = new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.dismiss();
								}
							};
							Utils.sharedInstance().showDialog(
									SettingActivity.this,
									R.string.dialog_internet_title,
									R.string.dialog_internet_message_short,
									R.drawable.alert,
									R.string.dialog_internet_neg_btn,
									negButListener,
									R.string.dialog_internet_pos_btn,
									posButListener);
						} else {

							OnClickListener negButListener = new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.dismiss();
									new Updatedatabase().execute();

								}
							};
							OnClickListener posButListener = new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.dismiss();
								}
							};
							Utils.sharedInstance().showDialog(
									SettingActivity.this,
									R.string.tittle_alert_updatedatabase,
									R.string.mess_alert_updatedatabase,
									R.drawable.alert, R.string.confirm,
									negButListener, R.string.cancel,
									posButListener);

						}

						return false;
					}
				});

	}

	class Updatedatabase extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			pDialog = new ProgressDialog(SettingActivity.this);
			pDialog.setMessage(getResources().getString(
					R.string.process_download_database));
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {

			Connectify connect = new Connectify(SettingActivity.this);
			connect.getAllDishestoDatabase();
			connect.downloadUserImage();
			connect.downloadDishImage();
			connect.downloadTableImage();
			Utils.sharedInstance().unpackZip("/sdcard/restautant/", "user.zip");
			Utils.sharedInstance().unpackZip("/sdcard/restautant/", "dish.zip");
			Utils.sharedInstance()
					.unpackZip("/sdcard/restautant/", "table.zip");
			
			
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			pDialog.dismiss();
			super.onPostExecute(result);
		}
	}

}
